package datadasetest;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Select extends JInternalFrame {
	DefaultTableModel model;
	TableRowSorter<TableModel> tSoter;
	database db;
	
	ResultSet rs = null;
	ResultSetMetaData rsmd = null;
	PreparedStatement pstmt = null;
	Vector<String> v = null;
	Connection conn;
	modi m;
	input1 i;
	
	
	private JPanel panel;
	private JPanel panel_1;
	private JTextField input;
	private JButton btnNewButton;
	private JPanel panel_2;
	private JSeparator separator;
	private JScrollPane scrollPane;
	private JTable table;

	public Select(Connection c,database db) {
		
		super("Select", true, true, true, false);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 341, 260);
		this.conn = c;
		this.db=db;
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_2(), BorderLayout.CENTER);

	}

	public void columnadd() {
		if (table!=null) table=null;
		String sql = input.getText();
		try {    
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery(sql);// 리절트셋에 대한 부가정보를 갖는 객체얻어오기
			rsmd = rs.getMetaData();// 컬럼개쉬가져오기

			int colCount = rsmd.getColumnCount();
			String t[] = new String[colCount + 1];
			t[0] = "NO";

			for (int i = 1; i <= colCount; i++) {
				String colName = rsmd.getColumnName(i);
				t[i] = colName;
			}

			for(int i=0; i<=t.length-1; i++){
				System.out.println(t[i]);
			}
			
			model = new DefaultTableModel(t,0);
			scrollPane.setViewportView(getTable_1());

			int k=0; 
			while (rs.next()) {
				v= new Vector<String>();
				k=k+1;
				v.add(""+k);
				for (int j = 1; j <= colCount; j++) {
					v.add(rs.getString(j));
				}
				model.addRow(v);
			}
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void setD(modi m) {
		this.m = m;
	}
//	public void setI(Insert i) {
	//	//this.i = i;
	//}
	//public void setD2(Delete d){
	//	this.d=d;
//	}
//	public void setO(Delete o){
	//	//this.d=d;
//	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getPanel_1(), BorderLayout.NORTH);
		}
		return panel;
	}

	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getInput(), BorderLayout.CENTER);
			panel_1.add(getButton_1(), BorderLayout.EAST);
		}
		return panel_1;
	}

	private JTextField getInput() {
		if (input == null) {
			input = new JTextField();
			input.setColumns(10);
		}
		return input;
	}

	private JButton getButton_1() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					columnadd();
				}
			});
		}
		return btnNewButton;
	}

	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setForeground(Color.WHITE);
			panel_2.setLayout(new BorderLayout(0, 0));
			panel_2.add(getSeparator(), BorderLayout.NORTH);
			panel_2.add(getScrollPane(), BorderLayout.CENTER);
		}
		return panel_2;
	}

	private JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
			separator.setPreferredSize(new Dimension(5, 5));
			separator.setBorder(null);
		}
		return separator;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();

		}
		return scrollPane;
	}

	private JTable getTable_1() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(db.mo == null){
						db.openupdate();
					}else{
						db.mo.setVisible(true);
					}
					
					//if(db.del == null){
					///	db.openDelete();
					//}else{
					//	db.delete.setVisible(true);
					//}
					
					int a=table.getSelectedRow();
					String e=String.valueOf(model.getValueAt(a, 1));//코드
					String f=(String)String.valueOf(model.getValueAt(a, 2));//ea
					String g=(String)String.valueOf(model.getValueAt(a, 3));//시리얼
					
					
					db.mo.setData(e, f, g);
						
				
				}
			});
			tSoter = new TableRowSorter(model);
			table.setRowSorter(tSoter);

		}
		return table;
	}
}
